//
//  XFXSportViewController.m
//  XFXnews
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXSportViewController.h"
#import "XFXNewsHeadLineModel.h"
#import "XFXRecreationTopView.h"
#import "XFXNewsHeadLineView.h"
#import "XFXNewsHeadLineOneTableViewCell.h"
#import "XFXNewsHeadLineTableViewCell.h"
#import "XFXReadImageDetailViewController.h"
#import "XFXReadDetailViewController.h"
#import "XFXRecreationTableViewCell.h"
#import "XFXSportOneTableViewCell.h"
@interface XFXSportViewController ()<UITableViewDataSource,UITableViewDelegate,XFXNewsHeadLineViewDelegate,XFXSportOneTableViewCellDelegate>

@property (nonatomic, retain) XFXNewsHeadLineView *newsHeadLineViews;
@property (nonatomic, retain) NSMutableArray *headerArray;
@property (nonatomic, retain) NSMutableArray *allArray;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *imgsrc;

@end

@implementation XFXSportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blueColor];
    
    self.view.backgroundColor = [UIColor greenColor];
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[XFXSportOneTableViewCell class] forCellReuseIdentifier:@"cellOne"];
    [self.myTableView registerClass:[XFXNewsHeadLineTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_myTableView];
    self.headerArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    self.isUpLoading = NO;
    [self addHeader];
    [self addFooter];
    [self.myTableView reloadData];
    
}

// 下拉刷新
- (void)addHeader{
    
    __block XFXSportViewController *vc = self;
    [self.myTableView addHeaderWithCallback:^{
        vc.page = 0;
        vc.isUpLoading = NO;
        [vc getJson:vc.page];
    }];
    
    [self.myTableView headerBeginRefreshing];
}

// 上拉刷新
- (void)addFooter{
    
    __block XFXSportViewController *vc = self;
    
    [self.myTableView addFooterWithCallback:^{
        vc.page++;
        if (vc.page <= 20) {
            vc.isUpLoading = YES;
            [vc getJson:vc.page];
        }
        
    }];
}

// 数据请求
- (void)getJson:(NSInteger)page{
    
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348649079062/%ld-20.html",page * 20];
    
    [XFXAFNAppTools getNewsWithHeadline:str block:^(id reulst) {
        if (self.isUpLoading == NO) {
            [self.allArray removeAllObjects];
            [self.headerArray removeAllObjects];
        }
        NSMutableArray *array = [reulst objectForKey:@"T1348649079062"];
        NSMutableDictionary *dic = [array objectAtIndex:0];
        NSMutableArray *adsArray = [dic objectForKey:@"ads"];
        for (NSDictionary *dic in adsArray) {
            
            XFXNewsHeadLineModel *model = [[XFXNewsHeadLineModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.headerArray addObject:model];
            
        }
        for (NSDictionary *dic in array) {
            XFXNewsHeadLineModel *model = [[XFXNewsHeadLineModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];
        }
        [self.allArray removeObjectAtIndex:0];
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        [self.myTableView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return XFXHEIGHT * 0.28;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XFXNewsHeadLineModel *headerModel = [self.allArray objectAtIndex:indexPath.row];
    if (headerModel.imgnewextra != nil) {
        return XFXHEIGHT * 0.19;
    }else {
        return XFXHEIGHT * 0.18;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    self.newsHeadLineViews = [[XFXNewsHeadLineView alloc] initWithFrame:CGRectMake(0, 0, XFXWIDTH, XFXHEIGHT * 0.4)];
    self.newsHeadLineViews.backgroundColor = [UIColor redColor];
    self.newsHeadLineViews.array = self.allArray;
    self.newsHeadLineViews.myDelegate = self;
    return self.newsHeadLineViews;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allArray.count;
    
}

// 自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XFXNewsHeadLineModel *headerModel = [self.allArray objectAtIndex:indexPath.row];
    if ([headerModel.skipType isEqualToString:@"photoset"]) {
        XFXSportOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellOne"];
        cell.myDelegate = self;
        cell.model = headerModel;
        return cell;
    }else {
        XFXNewsHeadLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.modelOne = headerModel;
        return cell;
    }
}

// cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XFXNewsHeadLineModel *headerModel = [self.allArray objectAtIndex:indexPath.row];
    
    if ([headerModel.skipType isEqualToString:@"photoset"]) {
        XFXReadImageDetailViewController *readImageViews = [[XFXReadImageDetailViewController alloc] init];
        readImageViews.model = headerModel;
        readImageViews.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:readImageViews animated:YES];
    }else{
        XFXReadDetailViewController *detail = [[XFXReadDetailViewController alloc] init];
        detail.model = headerModel;
        detail.hidesBottomBarWhenPushed = NO;
        detail.navigationController.navigationBar.translucent = NO;
        [self.navigationController pushViewController:detail animated:YES];
    }
}


// 协议方法
-(void)showpushDetail:(XFXNewsHeadLineModel *)model{
    
    XFXReadImageDetailViewController *readImageViews = [[XFXReadImageDetailViewController alloc] init];
    readImageViews.model = model;
    readImageViews.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:readImageViews animated:YES];
    
}

// 协议方法
- (void)pushDetail:(XFXNewsHeadLineModel *)mdoel{
    
    XFXReadImageDetailViewController *detael = [[XFXReadImageDetailViewController alloc] init];
    detael.model = mdoel;
    detael.hidesBottomBarWhenPushed = YES;
    detael.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:detael animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
